'use strict';

export default class extends think.service.base {
  /**
   * init
   * @return {}         []
   */
  init(...args){
    super.init(...args);
    let options  = args[0] || {};
    this.sid     = options.sid
    this.ip      = options.ip
    this.time    = options.time
    this.nick    = options.nick
    this.pg      = options.pg
    this.rg      = options.rg
    this.uid     = options.uid
    this.token   = options.token
    this.cansend = false;
  }
  
  * check(data){
    data = data || {};
    let rules = {
        uid  : data.uid    || this.uid,
        nick : decodeURI(data.nick   || this.nick),
        ip   : data.ip     || this.ip,
        pg   : data.pg     || this.pg,
        rg   : data.rg     || this.rg,
        token: data.token  || this.token
    };
    let token = yield this._md5([
        rules.uid,
        rules.nick,
        rules.ip,
        rules.pg,
        rules.rg,
        "qie/video"
    ].join(''));
    if(token === rules.token){
        Object.assign(this, rules);
        return this.cansend = true;
    }else{
        return this.cansend = false;
    }
  }

  * _md5(str){
    str = String(str);
    let crypto = yield think.npm('crypto');
    let md5sum = crypto.createHash('md5');
    md5sum.update(str);
    return md5sum.digest('hex');
  }
}

// class User {
//     constructor(options){
//         this.sid     = options.sid
//         this.ip      = options.ip
//         this.time    = options.time
//         this.nick    = options.nick
//         this.pg      = options.pg
//         this.rg      = options.rg
//         this.uid     = options.uid
//         this.cansend = false;
//     }
// }
